package com.wl.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.entity.Delivery_07;
import com.wl.entity.Loginfo;
import com.wl.entity.Warehouse_06;
import com.wl.service.Delivery_07Service;
import com.wl.service.Warehouse_06Service;
import com.wl.util.PageData;

/**
 * Warehouse_06控制器
 * 
 * @author HCH
 *
 */
@Controller
public class Warehouse_06Controller {

	// 注入Warehouse_06Service
	@Autowired
	private Warehouse_06Service warehouseService;

	// 注入Delivery_07Service
	@Autowired
	private Delivery_07Service deliveryService;

	/**
	 * 动态sql语句添加业务信息
	 * 
	 * @return
	 */
	@PostMapping(value = "addwar", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean testInsertSelective(@RequestBody Warehouse_06 record) {
		// appstatus默认为1，表示尚未处理
		record.setAppstatus(1);
		// 调用insertSelective()方法
		boolean flag = warehouseService.insertSelective(record);
		return flag;
	}

	/**
	 * 根据当前处理人编号查找尚未处理的业务并分页
	 * 
	 * @param page
	 * @param limit
	 * @param appno
	 * @return
	 */
	@GetMapping(value = "getwarbyappno", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Warehouse_06> selectWarehouseByAppno(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "appno", defaultValue = "") String appno) {
		PageData<Warehouse_06> pageData = warehouseService.selectWarehouseByAppno(page, limit, appno);
		return pageData;
	}

	/**
	 * 根据flowid动态修改业务信息
	 * 
	 * @param record
	 * @return
	 */
	@PutMapping(value = "updwarbyid", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateByPrimaryKeySelective(@RequestBody Warehouse_06 record) {
		// 获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String datetimenow = df.format(new Date());
		// 将当系统日期和时间赋值给对象
		record.setApprovetime(datetimenow);
		// 处理意见参数由前台传递
		// record.setAppresult("");
		// 修改处理状态为0，表示已处理
		record.setAppstatus(0);
		// 调用updateByPrimaryKeySelective()方法
		boolean flag = warehouseService.updateByPrimaryKeySelective(record);
		return flag;
	}

	/**
	 * 处理当前业务，并提交给下一环节
	 * 
	 * @param flowid
	 * @param applyno
	 * @param appresult
	 * @param deliveryno
	 * @return
	 */
	@PostMapping(value = "waranddel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean warehouseAndDelivery(@RequestParam("flowid") String flowid, @RequestParam("appno") String appno,
			@RequestParam("applyno") String applyno, @RequestParam("appresult") String appresult,
			@RequestParam("deliveryno") String deliveryno) {
		// 获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String datetimenow = df.format(new Date());
		// 构建Warehouse_06对象
		Warehouse_06 warehouse = new Warehouse_06();
		// 将当系统日期和时间赋值给对象
		warehouse.setApprovetime(datetimenow);
		// 处理意见参数由前台传递
		// record.setAppresult("");
		// 修改处理状态为0，表示已处理
		warehouse.setAppstatus(0);
		warehouse.setApplyno(applyno);
		warehouse.setFlowid(flowid);
		warehouse.setAppresult(appresult);
		// 构建Delivery_07对象
		Delivery_07 delivery = new Delivery_07();
		delivery.setDeliveryno(deliveryno);
		delivery.setDeliverystatus(1);
		delivery.setApplyno(applyno);
		delivery.setFlowid(flowid);
		// 创建Loginfo对象 用于增加日志
		Loginfo log = new Loginfo();
		log.setPerson(appno);
		log.setFlowid(flowid);
		log.setLogcontent(appresult);
		log.setLogdate(datetimenow);
		// 调用service
		boolean flag = warehouseService.warehouseAndDelivery(warehouse, delivery, log);
		return flag;
	}

	/**
	 * 根据当前处理人编号查找业务并分页
	 * 
	 * @param page
	 * @param limit
	 * @param appno
	 * @return
	 */
	@GetMapping(value = "getwarbypage", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Warehouse_06> selectWarehouseByPage(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "flowid", defaultValue = "") String flowid,
			@RequestParam(value = "appno", defaultValue = "") String appno,
			@RequestParam(value = "appresult", defaultValue = "") String appresult,
			@RequestParam(value = "starttime", defaultValue = "") String starttime,
			@RequestParam(value = "endtime", defaultValue = "") String endtime,
			@RequestParam(value = "appstatus", defaultValue = "") Integer appstatus,
			@RequestParam(value = "applyno", defaultValue = "") String applyno) {
		PageData<Warehouse_06> pageData = warehouseService.selectWarehouseByPage(page, limit, flowid, appno, appresult,
				starttime, endtime, appstatus, applyno);
		return pageData;
	}

	/**
	 * 根据flowid查找
	 * 
	 * @param flowid
	 * @return
	 */
	@GetMapping(value = "selectWarByFlowid", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Warehouse_06 selectWarByFlowid(@RequestParam(value = "flowid") String flowid) {
		return warehouseService.selectByFlowid(flowid);
	}
}
